home *** CD-ROM | disk | FTP | other *** search
/ Aminet 22 / Aminet 22 (1997)(GTI - Schatztruhe)[!][Dec 1997].iso / Aminet / dev / src / ConfigFileSrc.lha / ConfigFileSrc12 / Library / Funcs / New.c < prev    next >
Encoding:
Text File  |  1997-10-02  |  9.9 KB  |  387 lines

  1. /*
  2. **        $PROJECT: ConfigFile.library
  3. **        $FILE: New.c
  4. **        $DESCRIPTION: cf_New#?() functions
  5. **
  6. **        (C) Copyright 1996-1997 Marcel Karas
  7. **             All Rights Reserved.
  8. */
  9.  
  10. IMPORT struct ExecBase    * SysBase;
  11.  
  12. /****** configfile.library/cf_NewArgument ************************************
  13. *
  14. *   NAME
  15. *        cf_NewArgument -- Creates a new argument node.
  16. *
  17. *   SYNOPSIS
  18. *        ArgNode = cf_NewArgument(GrpNode,Name);
  19. *        D0                       A0      A1
  20. *
  21. *        CFArgument * cf_NewArgument(CFGroup *,STRPTR);
  22. *
  23. *   FUNCTION
  24. *        This function creates a new argument node. The GrpNode must be a
  25. *        pointer to a group node. 
  26. *
  27. *   INPUTS
  28. *        GrpNode - The group node for add to. (!!! not NULL !!!)
  29. *        Name - The name of the new argument node.
  30. *
  31. *   RESULT
  32. *        ArgNode - The new argument node or NULL by failure.
  33. *
  34. *   EXAMPLE
  35. *        CFGroup    * myGrpNode;
  36. *        CFArgument * myArgNode;
  37. *
  38. *        ...
  39. *        
  40. *        myArgNode = cf_NewArgument(myGrpNode,"ExampleArg");
  41. *        ...
  42. *
  43. *        In the CF file:
  44. *
  45. *        ...
  46. *        ExampleArg=
  47. *        ...
  48. *
  49. *   NOTES
  50. *        The version 2 of the ConfigFile.library didn't support anymore a
  51. *        NULL pointer by GrpNode.
  52. *
  53. *   SEE ALSO
  54. *        cf_NewGroup(), cf_NewItem(), cf_NewArgItem()
  55. *
  56. ******************************************************************************
  57. *
  58. */
  59.  
  60. LibCall iCFArgument * cf_NewArgument ( REGA0 iCFGroup * GrpNode , REGA1 STRPTR Name )
  61. {
  62.     if ( GrpNode )
  63.     {
  64.         GrpNode->Header->Flags |= CF_HFLG_CHANGED;
  65.  
  66.         return (NewArg (GrpNode, Name, StrLen (Name)));
  67.     }
  68.  
  69.     return (NULL);
  70. }
  71.  
  72.     /* NewArg():
  73.      *
  74.      *    create a new Argument.
  75.      */
  76.  
  77. IMPORT struct DosLibrary    * DOSBase;
  78.  
  79. iCFArgument * NewArg ( iCFGroup * GrpNode , STRPTR Name , ULONG Length )
  80. {
  81.     iCFArgument *ArgNode;
  82.     UBYTE             StructLen = sizeof (iCFArgument) + Length + 2;
  83.  
  84.     ArgNode = MyAllocPooled (GrpNode->Header->MemPool, StructLen);
  85.  
  86.     ArgNode->Name                = (STRPTR) ( (ULONG) ArgNode + sizeof (iCFArgument) );
  87.     ArgNode->Name[0]            = Length;
  88.     ArgNode->Name ++;
  89.     ArgNode->Name[Length]    = 0;
  90.  
  91.     ArgNode->GrpNode        = GrpNode;
  92.     ArgNode->StructSize    = StructLen;
  93.  
  94.     ArgNode->ExtFlags        = 0;
  95.  
  96.     MemCpy (ArgNode->Name, Name, Length);
  97.     NewList ((struct List *) &ArgNode->ItemList);
  98.     AddTail ((struct List *) &GrpNode->ArgList, (struct Node *) ArgNode);
  99.  
  100.     return (ArgNode);
  101. }
  102.  
  103. /****** configfile.library/cf_NewGroup ***************************************
  104. *
  105. *   NAME
  106. *        cf_NewGroup -- Creates a new group node.
  107. *
  108. *   SYNOPSIS
  109. *        GrpNode = cf_NewGroup(Header,Name);
  110. *        D0                    A0    A1
  111. *
  112. *        CFGroup * cf_NewGroup(CFHeader *,STRPTR);
  113. *
  114. *   FUNCTION
  115. *        This function creates a new group node. The Header must be a
  116. *        pointer to a CFHeader structure.
  117. *
  118. *   INPUTS
  119. *        Header - Pointer to the CFHeader structure for add to.
  120. *                 (!!! not NULL !!!)
  121. *        Name - Name of the new group node.
  122. *
  123. *   RESULT
  124. *        GrpNode - The new group node or NULL by failure.
  125. *
  126. *   EXAMPLE
  127. *        CFHeader * myHeader;
  128. *        CFGroup * myGrpNode;
  129. *
  130. *        ...
  131. *        
  132. *        myGrpNode = cf_NewGroup(myHeader,"ExampleGroup");
  133. *        cf_NewArgument(myGrpNode,"ExampleArg");
  134. *        ...
  135. *
  136. *        In the CF file:
  137. *
  138. *        ...
  139. *        [ExampleGroup]
  140. *
  141. *        ExampleArg=
  142. *        ...
  143. *
  144. *   NOTES
  145. *        The version 2 of the ConfigFile.library didn't support anymore a
  146. *        NULL pointer by Header.
  147. *
  148. *   SEE ALSO
  149. *        cf_NewArgument(), cf_NewItem(), cf_NewArgItem()
  150. *
  151. ******************************************************************************
  152. *
  153. */
  154.  
  155. LibCall iCFGroup * cf_NewGroup ( REGA0 iCFHeader * Header , REGA1 STRPTR Name )
  156. {
  157.     if ( Header )
  158.     {
  159.         Header->Flags |= CF_HFLG_CHANGED;
  160.  
  161.         return (NewGrp (Header, Name, StrLen (Name)));
  162.     }
  163.  
  164.     return (NULL);
  165. }
  166.  
  167.     /* NewGrp():
  168.      *
  169.      *    create a new Group.
  170.      */
  171.  
  172. iCFGroup * NewGrp ( iCFHeader * Header , STRPTR Name , ULONG Length )
  173. {
  174.     iCFGroup *GrpNode;
  175.     UBYTE         StructLen = sizeof (iCFGroup) + Length + 2;
  176.     
  177.     GrpNode = MyAllocPooled (Header->MemPool, StructLen);
  178.  
  179.     GrpNode->Name                = (STRPTR) ( (ULONG) GrpNode + sizeof (iCFGroup) );
  180.     GrpNode->Name[0]            = Length;
  181.     GrpNode->Name ++;
  182.     GrpNode->Name[Length]    = 0;
  183.  
  184.     GrpNode->Header        = Header;
  185.     GrpNode->StructSize    = StructLen;
  186.  
  187.     GrpNode->ExtFlags        = 0;
  188.  
  189.     MemCpy (GrpNode->Name, Name, Length);
  190.     NewList ((struct List *) &GrpNode->ArgList);
  191.     AddTail ((struct List *) &Header->GroupList, (struct Node *) GrpNode);
  192.  
  193.     return (GrpNode);
  194. }
  195.  
  196. /****** configfile.library/cf_NewItem ****************************************
  197. *
  198. *   NAME
  199. *        cf_NewItem -- Creates a new item node.
  200. *
  201. *   SYNOPSIS
  202. *        ItemNode = cf_NewItem(ArgNode,Contents,Type,SpecialType);
  203. *        D0                    A0      D0       D1   D2
  204. *
  205. *        CFItem * cf_NewItem(CFArgument *,LONG,ULONG,ULONG);
  206. *
  207. *   FUNCTION
  208. *        This function creates a new item node. The ArgNode must be a
  209. *        pointer to a argument node.
  210. *
  211. *   INPUTS
  212. *        ArgNode - The argument node for add to. (!!! not NULL !!!)
  213. *        Contents - The contents of the new item node.
  214. *        Type - Type of the contents.
  215. *
  216. *           CF_ITYP_STRING -- String type (Contents is a pointer to a
  217. *                             NULL-terminated string)
  218. *           CF_ITYP_NUMBER -- Number type (Contents is long value e.g.
  219. *                             44253 or -23456)
  220. *           CF_ITYP_BOOL   -- Bool type   (Contents is long value TRUE or
  221. *                             FALSE)
  222. *        SpecialType - Special types for cf_Write() or NULL for default.
  223. *
  224. *           CF_ITYP_BOOL:
  225. *
  226. *             CF_STYP_BOOL_YES  -- "YES/NO"
  227. *             CF_STYP_BOOL_TRUE -- "TRUE/FALSE"
  228. *             CF_STYP_BOOL_ON   -- "ON/OFF"
  229. *
  230. *           CF_ITYP_NUMBER:
  231. *
  232. *             CF_STYP_NUM_DEC   -- Decimal (e.g 24574)
  233. *             CF_STYP_NUM_HEX   -- Hexdecimal (e.g. $fDe2)
  234. *             CF_STYP_NUM_BIN   -- Binary (e.g. %10111)
  235. *
  236. *   RESULT
  237. *        ItemNode - The new group node or NULL by failure.
  238. *
  239. *   EXAMPLE
  240. *        CFArgument * myArgNode;
  241. *
  242. *        ...
  243. *        
  244. *        myArgNode = cf_NewArgument(NULL,"ExampleArg");
  245. *        cf_NewItem(myArgNode,(LONG)"Foo Str",CF_ITYP_STRING,NULL);
  246. *        cf_NewItem(myArgNode,5467,CF_ITYP_NUMBER,CF_STYP_NUM_DEC);
  247. *        cf_NewItem(myArgNode,35678,CF_ITYP_NUMBER,CF_STYP_NUM_HEX);
  248. *        cf_NewItem(myArgNode,23,CF_ITYP_NUMBER,CF_STYP_NUM_BIN);
  249. *        cf_NewItem(myArgNode,FALSE,CF_ITYP_BOOL,CF_STYP_NUM_ON);
  250. *        cf_NewItem(myArgNode,TRUE,CF_ITYP_BOOL,CF_STYP_NUM_ON);
  251. *        cf_NewItem(myArgNode,TRUE,CF_ITYP_BOOL,CF_STYP_NUM_YES);
  252. *        ...
  253. *
  254. *        In the CF file:
  255. *
  256. *        ...
  257. *        ExampleArg="Foo Str",5467,$865E,%10111,OFF,ON,YES
  258. *        ...
  259. *
  260. *   NOTES
  261. *        The version 2 of the ConfigFile.library didn't support anymore a
  262. *        NULL pointer by ArgNode.
  263. *
  264. *   SEE ALSO
  265. *        cf_NewArgument(), cf_NewGroup(), cf_Write(), cf_NewArgItem(),
  266. *        <libraries/configfile.h>
  267. *
  268. ******************************************************************************
  269. *
  270. */
  271.  
  272. LibCall iCFItem * cf_NewItem ( REGA0 iCFArgument * ArgNode ,
  273.             REGD0 LONG Contents , REGD1 ULONG Type , REGD2 ULONG SpecialType )
  274. {
  275.     iCFItem *ItemNode = NULL;
  276.     APTR        MemPool;
  277.     UBYTE        Length, StructLen = sizeof (iCFItem);
  278.  
  279.     if ( ArgNode)
  280.     {
  281.         MemPool = ArgNode->GrpNode->Header->MemPool;
  282.  
  283.         ArgNode->GrpNode->Header->Flags |= CF_HFLG_CHANGED;
  284.  
  285.         if ( Type == CF_ITYP_STRING )
  286.             StructLen += 2 + ( Length = StrLen ((STRPTR) Contents) );
  287.  
  288.         ItemNode = MyAllocPooled (MemPool, StructLen);
  289.  
  290.         ItemNode->ArgNode            = ArgNode;
  291.         ItemNode->StructSize        = StructLen;
  292.         ItemNode->Type                = Type;
  293.         ItemNode->SpecialType    = SpecialType ? SpecialType : CF_STYP_NUM_DEC;
  294.         ItemNode->ExtFlags        = 0;
  295.  
  296.         if ( Type == CF_ITYP_STRING )
  297.         {
  298.             ItemNode->Contents.String        = (STRPTR) ( (ULONG) ItemNode + sizeof (iCFItem) );
  299.             ItemNode->Contents.String[0]    = Length;
  300.             ItemNode->Contents.String ++;
  301.             ItemNode->Contents.String[Length]    = 0;
  302.  
  303.             MemCpy (ItemNode->Contents.String, (STRPTR) Contents, Length);
  304.         }
  305.         else if ( Type == CF_ITYP_BOOL )
  306.             ItemNode->Contents.Bool        = Contents ? TRUE : FALSE;
  307.  
  308.         else if ( Type == CF_ITYP_NUMBER )
  309.             ItemNode->Contents.Number    = Contents;
  310.  
  311.         else
  312.         {
  313.             MyFreePooled (MemPool, ItemNode, sizeof(iCFItem));
  314.  
  315.             return (NULL);
  316.         }
  317.  
  318.         AddTail ((struct List *) &ArgNode->ItemList, (struct Node *) ItemNode);
  319.     }
  320.  
  321.     return (ItemNode);
  322. }
  323.  
  324. /****** configfile.library/cf_NewArgItem *************************************
  325. *
  326. *   NAME
  327. *        cf_NewArgItem -- Creates a new argument node and a new item node.
  328. *
  329. *   SYNOPSIS
  330. *        ArgNode = cf_NewArgItem(GrpNode,Name,Contents,Type,SpecialType);
  331. *        D0                      A0      A1   D0       D1   D2
  332. *
  333. *        CFArgument * cf_NewArgItem(CFGroup *,STRPTR,LONG,ULONG,ULONG);
  334. *
  335. *   FUNCTION
  336. *        This function creates a new argument node and a new item node. The
  337. *        GrpNode must be a pointer to a group node.
  338. *
  339. *   INPUTS
  340. *        GrpNode - The group node for add to. (!!! not NULL !!!)
  341. *        Name - The name of the new argument node.
  342. *        Contents - The contents of the new item node.
  343. *        Type - Type of the contents.
  344. *        SpecialType - Special types for cf_Write() or NULL for default.
  345. *
  346. *   RESULT
  347. *        ArgNode - The new argument node or NULL by failure.
  348. *
  349. *   EXAMPLE
  350. *        CFGroup * myGrpNode;
  351. *
  352. *        ...
  353. *        
  354. *        cf_NewArgument(myGrpNode,"ExampleArg","FooStr",CF_ITYP_STRING,NULL);
  355. *        ...
  356. *
  357. *        In the CF file:
  358. *
  359. *        ...
  360. *        ExampleArg="FooStr"
  361. *        ...
  362. *
  363. *   NOTES
  364. *        The Version 2 of the ConfigFile.library don't support anymore a
  365. *        NULL pointer by GrpNode.
  366. *
  367. *   SEE ALSO
  368. *        cf_NewGroup(), cf_NewItem(), cf_NewArgument()
  369. *
  370. ******************************************************************************
  371. *
  372. */
  373.  
  374. SLibCall iCFArgument * cf_NewArgItem ( REGA0 iCFGroup * GrpNode ,
  375.     REGA1 STRPTR Name , REGD0 LONG Contents , REGD1 ULONG Type , REGD2 ULONG SpecialType )
  376. {
  377.     iCFArgument *NewArgNode = 0;
  378.  
  379.     if ( GrpNode )
  380.     {
  381.         NewArgNode = cf_NewArgument (GrpNode, Name);
  382.         cf_NewItem (NewArgNode, Contents, Type, SpecialType);
  383.     }
  384.  
  385.     return (NewArgNode);
  386. }
  387.